package com.qf.web.controller;

import com.qf.service.UserService;
import com.qf.service.impl.UserServiceImpl;
import com.qf.util.Base64Util;
import com.qf.util.StringUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/activate")
public class ActivateServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");//设置编码
        //获取前端数据
        String email = request.getParameter("e");//邮箱
        String code = request.getParameter("c");//激活码
        if(StringUtil.isEmpty(email) || StringUtil.isEmpty(code)){
            request.setAttribute("msg","激活失败");
            request.getRequestDispatcher("/message.jsp").forward(request,response);
            return;
        }
        //解码
        email = Base64Util.decode(email);
        code = Base64Util.decode(code);
        //激活
        UserService userService = new UserServiceImpl();
        try {
            userService.activate(email,code);
            request.setAttribute("msg","激活成功");
            request.getRequestDispatcher("/message.jsp").forward(request,response);

        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("msg","激活失败");
            request.getRequestDispatcher("/message.jsp").forward(request,response);

        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
